import { NPopselect, type PopselectProps } from "naive-ui";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";
export type PopselectFactoryConstructorType = {
  props?: PopselectProps;
  attrs?: HTMLAttributes;
  defaultSlot?: BaseComponentType;
  emptySlot?: BaseComponentType;
  actionSlot?: BaseComponentType;
  headerSlot?: BaseComponentType;
};

export class PopselectFactory {
  private attrs: HTMLAttributes = {};
  private props: PopselectProps = {};
  private defaultSlot: BaseComponentType = null;
  private emptySlot: BaseComponentType = null;
  private actionSlot: BaseComponentType = null;
  private headerSlot: BaseComponentType = null;

  constructor(param?: PopselectFactoryConstructorType) {
    if (param?.props) this.setProps(param.props);
    if (param?.attrs) this.setAttrs(param.attrs);
    if (param?.defaultSlot) this.setDefault(param.defaultSlot);
    if (param?.emptySlot) this.setEmpty(param.emptySlot);
    if (param?.actionSlot) this.setAction(param.actionSlot);
    if (param?.headerSlot) this.setHeader(param.headerSlot);
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }
  setProps(props: PopselectProps) {
    this.props = props;
    return this;
  }
  setDefault(defaultSlot: BaseComponentType) {
    this.defaultSlot = defaultSlot;
    return this;
  }
  setEmpty(emptySlot: BaseComponentType) {
    this.emptySlot = emptySlot;
    return this;
  }
  setAction(actionSlot: BaseComponentType) {
    this.actionSlot = actionSlot;
    return this;
  }
  setHeader(headerSlot: BaseComponentType) {
    this.headerSlot = headerSlot;
    return this;
  }

  create() {
    return (
      <NPopselect {...this.attrs} {...this.props}>
        {{
          default:
            this.defaultSlot != null
              ? () => getSlotsDom(this.defaultSlot)
              : null,

          empty:
            this.emptySlot != null ? () => getSlotsDom(this.emptySlot) : null,
          action:
            this.actionSlot != null ? () => getSlotsDom(this.actionSlot) : null,
          header:
            this.headerSlot != null ? () => getSlotsDom(this.headerSlot) : null,
        }}
      </NPopselect>
    );
  }
}
